<?php
namespace Labour\Controller;
use Think\Controller;
class IndexController extends Controller {
	//企业号corpid
	private $corpid = null;
	//企业号corpsecret
    private $corpsecret = null;

	public function __construct(){
        parent::__construct();
        $this -> corpid = C('CORPID');
        $this -> corpsecret  = C('SECRET');
        //echo createAuthUrl($this -> corpid,'Labour','Index','index');die;
	}
	//获取Access_token
	public function Access_token($corpid,$corpsecret){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret";
        $access_token = json_decode(file_get_contents($url),true)['access_token'];
        session('access_token',$access_token);
        return $access_token;
	}

	//获取用户userid
	public function Getuserid(){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".session('access_token')."&code=".session('code');
        $userid = json_decode(file_get_contents($url),true)['UserId'];
        return $userid;
	}

	//获取用户详细信息
	public function Getinfo($access_token,$userid){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".session(access_token)."&userid=$userid";
        $userinfo = json_decode(file_get_contents($url),true);
        return $userinfo;
	}

	//通过部门ID 获取部门名称
	public function Getbumen($id){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".session(access_token)."&id=$id";
        $bumen = json_decode(file_get_contents($url),true);
        return $bumen;
	}

	//获取Code 通过Code 获取用户名  当作条件去数据库里取相应数据
	public function index(){
        session_start();
        $code = $_GET['code'];
        session('code',$code);
        $access_token = $this -> Access_token($this -> corpid, $this -> corpsecret );

        $userid = 'HanYue';
        //$userid = $this -> Getuserid();
        session('userid',$userid);
        $Model = M('lb');
        $res = $Model -> where("yuangongbianhao = '$userid' ") -> select();
        $res = end($res);

        $this -> assign('data',$res); 
        $this -> display();
    }

    //按年份查询相应数据  查询出结果可能是多个 默认取最后一个
    public function search(){
        $keywords = $_GET['keywords'];
        $Model = M('lb');
        $userid = session('userid');
        $res = $Model -> where("niandu = '$keywords' AND yuangongbianhao = '$userid' ") -> select();
        $this -> assign('datas',end($res)); 
        $this -> assign('keywords',$keywords);
        $this -> display();
    }
}
